// pages/orderTraceability/index.js
// pages/user/myorder.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenLoading: false,
    userInfo: {},
    order_type: -1,
    orderList: [],
    thh_index: null,
    thh_hiddenmodalput: true,
    reasonInp: null,
    mobileInp: null,
    show: false,
    steps: [{
        text: '步骤一',
        desc: '描述信息',
        // inactiveIcon: 'location-o',
        // activeIcon: 'success',
      },
      {
        text: '步骤二',
        desc: '描述信息',
        // inactiveIcon: 'like-o',
        // activeIcon: 'plus',
      },
      {
        text: '步骤三',
        desc: '描述信息',
        // inactiveIcon: 'star-o',
        // activeIcon: 'cross',
      },
      {
        text: '步骤四',
        desc: '描述信息',
        // inactiveIcon: 'phone-o',
        // activeIcon: 'fail',
      },
    ],
    goodsType: 2,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, 'onLoad')
    let that = this
    if (options.q && options.q != "undefined") {
      const qrUrl = decodeURIComponent(options.q)
      console.log(qrUrl, 'qrUrl');
      //此处就是我们要获取的参数 json，通过方法解析
      let jsonUrl = that.GetwxUrlParam(qrUrl);
      console.log(jsonUrl, 'jsonUrl');
      //比如我要得到id的值，直接取值即可
      let type = jsonUrl.type;
      //let id = 123456;
      that.setData({
        goodsType: type
      })

    }
  },

  //下拉刷新
  onPullDownRefresh: function () {
    this.getOrder();
    wx.stopPullDownRefresh();
  },
  GetwxUrlParam(url) {
    let theRequest = {};
    if (url.indexOf("#") != -1) {
      const str = url.split("#")[1];
      const strs = str.split("&");
      for (let i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
      }
    } else if (url.indexOf("?") != -1) {
      const str = url.split("?")[1];
      const strs = str.split("&");
      for (let i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (opt) {
    var that = this
    //用户授权登录
    app.login(function (userInfo) {

      that.setData({
        userInfo: userInfo
      });
      that.getOrder();
    });

  },

  //切换订单
  changeOrder: function (e) {
    var that = this;
    var otype = e.currentTarget.dataset.type
    that.setData({
      order_type: otype
    });
    that.getOrder();
  },

  //获取订单列表
  getOrder: function () {
    var that = this;
    var paraArr = new Array();
    paraArr['type'] = this.data.goodsType;
    paraArr['row'] = 10;
    paraArr['page'] = 1;
    paraArr['order_type'] = that.data.order_type == -1 ? '' : that.data.order_type;
    paraArr['user_id'] = that.data.userInfo.userID;

    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api/data_grid/traceList',
      data: {
        // state: paraArr['state'],
        row: paraArr['row'],
        order_type: paraArr['order_type'],
        type: paraArr['type'],
        page: paraArr['page'],
        user_id: paraArr['user_id'],
        sign: sign
      },
      method: 'GET',
      header: {},
      success: function (res) {
        console.log(res);
        if (res.statusCode == 200) {
          // res.data = []
          that.setData({
            orderList: res.data.data,
            hiddenLoading: true
          });
          if (!res.data.data.length && that.data.order_type == -1) {
            setTimeout(() => {
              wx.showModal({
                title: '提示',
                content: '您暂时还没溯源订单，点击确定返回首页',
                complete: (res) => {
                  if (res.cancel) {

                  }
                  if (res.confirm) {
                    wx.switchTab({
                      url: '/pages/index/index',
                    })
                  }
                }
              })
            }, 2000)
          }
        }


      }
    })
  },
  goCardRecord: function () {
    console.log(111)
    app.redirect('cardRecord/cardRecord')
  },
  //去付款
  goPay: function (e) {
    app.redirect('pay/index', 'sn=' + e.currentTarget.dataset.sn);
  },

  //去评价
  goComment: function (e) {
    app.redirect('user/mycomment', 'id=' + e.currentTarget.dataset.id);
  },

  //取消订单
  cancelOrder: function (e) {

  },

  //确认收货
  shOrder: function (e) {

  },

  //查看物流
  showSend: function (e) {
    var that = this;
    var m_id = e.currentTarget.dataset.id
    if (!m_id) return;
    var paraArr = new Array();
    paraArr['order_id'] = m_id;
    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api/comm_get/getSend',
      data: {
        order_id: paraArr['order_id'],
        sign: sign
      },
      method: 'GET',
      header: {},
      success: function (res) {
        wx.showModal({
          title: res.data.sendType,
          content: res.data.sendNo,
          showCancel: false,
          success: function (res) {}
        })
      }
    })
  },
  //查看物流
  showSends: function (e) {
    console.log(e)
    let item = e.currentTarget.dataset.item
    let goodsType = this.data.goodsType
    //  this.data.goodsType
    console.log(item)
    wx.navigateTo({
      url: `/pages/orderTraceability/traceabilitydetail?id=${item.order_sn}&goodsType=${goodsType}`,
    })
    // wx.navigateTo({
    //   url: `plugin://kdzs-plugin/logistics-list?num=${item.express_sn}&phone=${item.tel.substring(7)}`,
    // })
    return
    var that = this;
    var m_id = e.currentTarget.dataset.id
    if (!m_id) return;
    var paraArr = new Array();
    paraArr['order_id'] = m_id;
    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api/llgn/wuliu',
      data: {
        order_sn: paraArr['order_id'],
        // sign: sign
      },
      method: 'GET',
      header: {},
      success: function (res) {
        let arr = []
        res.data.data.forEach(element => {
          arr.push({
            text: element.context,
            desc: element.ftime
          })
        });
        that.setData({
          show: true,
          steps: arr
        });
        // wx.showModal({
        //   title: res.data.sendType,
        //   content: res.data.sendNo,
        //   showCancel: false,
        //   success: function (res) {}
        // })
      }
    })
  },
  // 关闭物流弹窗
  onClose() {
    this.setData({
      show: false
    });
  },

  //删除订单
  delOrder: function (e) {

  },
  // 控制退货弹窗
  meExchanges(e) {
    this.setData({
      thh_index: e.currentTarget.dataset.id,
      thh_hiddenmodalput: false
    })


  },
  // 关闭退货弹窗
  thh_cancel() {
    console.log(2222);
    this.setData({
      thh_hiddenmodalput: true
    })
  },
  // 确定退货
  thh_confirm() {
    var that = this;
    console.log(that.data.reasonInp, that.data.mobileInp);
    if (that.data.reasonInp == null) {
      wx.showToast({
        title: '请输入退换货原因',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (that.data.mobileInp == null) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
      return
    }
    var paraArr = new Array();
    paraArr['user_id'] = that.data.userInfo.userID;
    paraArr['order_id'] = that.data.thh_index;
    paraArr['reason'] = that.data.reasonInp;
    paraArr['mobile'] = that.data.mobileInp;
    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api/user/refund',
      data: {
        user_id: paraArr['user_id'],
        order_id: paraArr['order_id'],
        reason: paraArr['reason'],
        mobile: paraArr['mobile'],
        // sign: sign
      },
      method: 'POST',
      header: {},
      success: function (res) {
        console.log(res.data, '11111');
        if (res.data.code == 1001) {
          wx.showToast({
            title: '申请成功',
            icon: 'none',
            duration: 2000
          })
          wx.navigateTo({
            url: '/pages/user/myorder?type=4',
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
        that.setData({
          reasonInp: null,
          mobileInp: null,
          thh_hiddenmodalput: true
        })
      }
    })

  },
  //退货原因
  on_reason(e) {
    console.log(e.detail.value);
    this.setData({
      reasonInp: e.detail.value
    })
  },
  //退货手机号
  on_mobile(e) {
    this.setData({
      mobileInp: e.detail.value
    })
  },
})